package com.zxbg.service.impl.proportalServiceImpl;

import com.zxbg.dao.DepartmentDao;
import com.zxbg.dao.EmployeeDao;
import com.zxbg.dao.InfoBlockDao;
import com.zxbg.dao.NavMenuDao;
import com.zxbg.domain.*;
import com.zxbg.service.impl.loginServiceImpl.LoginServiceImpl;
import com.zxbg.service.impl.systemtoolsServiceImpl.InfoBlockServiceImpl;
import com.zxbg.service.impl.systemtoolsServiceImpl.OrganizationalStructureServiceImpl;
import com.zxbg.service.proportalService.PIndexService;
import com.zxbg.service.systemtoolsService.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PIndexServiceImpl implements PIndexService {
    @Autowired
    NavMenuDao navMenuDao;
    @Autowired
    RoleManagementService roleManagementService;
    @Autowired
    InfoBlockDao infoBlockDao;
    @Autowired
    InfoBlockServiceImpl infoBlockService;
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    OrganizationalStructureServiceImpl organizationalStructureService;
    @Autowired
    LoginServiceImpl loginService;

    @Override
    public List<NavMenu> getActiveNavMenu(String employee_id, Integer tenant_id) {
        List<NavMenu> navMenuList = navMenuDao.getActiveNavMenu(tenant_id);
        List<NavMenu> tempList = new ArrayList<>();
        boolean ifSysadmin = roleManagementService.getIfSysadmin(employee_id, tenant_id);
        for (NavMenu navMenu : navMenuList) {
            navMenu.setMenu_name(new Internationalization(navMenu.getMenu_name_zh(), navMenu.getMenu_name_en()));
            if(ifSysadmin) {
                tempList.add(navMenu);
            } else {
                if (!navMenu.getVisit_url().equals("/AppCenter")) {
                    tempList.add(navMenu);
                }
            }
        }
        return tempList;
    }

    @Override
    public List<InfoBlock> getActiveLunBoPic(String employee_id, Integer tenant_id) {
        List<InfoBlock> infoBlockList = infoBlockDao.getActiveLunBoPic(tenant_id);
        if(infoBlockList.size() > 0) {
            if(!employee_id.equals("sysadmin")) {
                infoBlockList = getPersonalInfoBlock(employee_id, infoBlockList, tenant_id);
            } else {
                for (InfoBlock infoBlock : infoBlockList) {
                    infoBlock.setCreator(organizationalStructureService.getAllKindsDept(employeeDao.getEmployeeByEmployeeId(infoBlock.getCreator_id(), tenant_id)));
                }
            }
        }
        return infoBlockList;
    }

    public List<InfoBlock> getPersonalInfoBlock(String employee_id, List<InfoBlock> infoBlockList, Integer tenant_id) {
        List<InfoBlock> resList = new ArrayList<>();
        for (InfoBlock infoBlock : infoBlockList) {
            String public_range = infoBlock.getPublic_range();
            infoBlock.setCreator(organizationalStructureService.getAllKindsDept(employeeDao.getEmployeeByEmployeeId(infoBlock.getCreator_id(), tenant_id)));
            if(public_range.equals("all")) {
                resList.add(infoBlock);
            } else {
                Map<String, Object> map = infoBlockService.getPublicRangeRealInfo(infoBlock, tenant_id);
                List<Department> departmentList = (List<Department>) map.get("dept");
                List<Employee> employeeList = (List<Employee>) map.get("employee");
                boolean ifAdd = false;
                for (Employee employee : employeeList) {
                    if(employee.getEmployee_id().equals(employee_id)) {
                        resList.add(infoBlock);
                        ifAdd = true;
                        break;
                    }
                }
                if(ifAdd || departmentList.size() == 0) {
                    continue;
                }
                String all_dept = employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id).getAll_dept_id();
                if(all_dept != null && !all_dept.equals("")) {
                    List<String> hasDeptList = Arrays.asList(all_dept.split(","));
                    Set<Integer> hasAllDeptList = new HashSet<>();
                    for (String dept : hasDeptList) {
                        List<Integer> lineUpDept = getLineUpDept(Integer.parseInt(dept));
                        hasAllDeptList.addAll(lineUpDept);
                    }
                    for (Department department : departmentList) {
                        if(hasAllDeptList.contains(department.getDept_id())) {
                            resList.add(infoBlock);
                            break;
                        }
                    }
                }
            }
        }
        return resList;
    }

    public List<Integer> getLineUpDept(Integer dept_id) {
        Department currentDept = departmentDao.getDeptById(dept_id);
        List<Integer> resList = new ArrayList<>();
        if(currentDept.getParent() != null && !currentDept.getParent().equals("")) {
            resList.addAll(getLineUpDept(currentDept.getParent()));
        }
        resList.add(dept_id);
        return resList;
    }

    @Override
    public boolean updateViewed(String employee_id, Integer id) {
        InfoBlock infoBlock = infoBlockDao.getInfoBlockById(id);
        String viewed = infoBlock.getViewed_employee_id();
        if(viewed == null) {
            infoBlock.setViewed_employee_id(employee_id);
        } else {
            List<String> viewedList = Arrays.asList(viewed.split(","));
            if(!viewedList.contains(employee_id)) {
                infoBlock.setViewed_employee_id(viewed + "," + employee_id);
            }
        }
        Integer click_num = infoBlock.getClick_num();
        if(click_num == null) {
            infoBlock.setClick_num(1);
        } else {
            infoBlock.setClick_num(click_num + 1);
        }
        return infoBlockDao.updateViewed(infoBlock) > 0;
    }

    @Override
    public List<InfoBlock> getActiveNews(String employee_id, Integer tenant_id) {
        List<InfoBlock> infoBlockList = infoBlockDao.getActiveNews(tenant_id);
        return getResInfoList(infoBlockList, employee_id, tenant_id);
    }

    @Override
    public List<InfoBlock> getActiveNotices(String employee_id, Integer tenant_id) {
        List<InfoBlock> infoBlockList = infoBlockDao.getActiveNotices(tenant_id);
        return getResInfoList(infoBlockList, employee_id, tenant_id);
    }

    public List<InfoBlock> getResInfoList(List<InfoBlock> infoBlockList, String employee_id, Integer tenant_id) {
        if(infoBlockList.size() > 0) {
            if(!employee_id.equals("sysadmin")) {
                infoBlockList = getPersonalInfoBlock(employee_id, infoBlockList, tenant_id);
            } else {
                for (InfoBlock infoBlock : infoBlockList) {
                    infoBlock.setCreator(organizationalStructureService.getAllKindsDept(employeeDao.getEmployeeByEmployeeId(infoBlock.getCreator_id(), tenant_id)));
                }
            }
        }
        return infoBlockService.getTopOrderList(infoBlockList);
    }

    @Override
    public List<InfoBlock> getActiveRegulation(String employee_id, Integer tenant_id) {
        List<InfoBlock> infoBlockList = infoBlockDao.getActiveRegulation(tenant_id);
        return getResInfoList(infoBlockList, employee_id, tenant_id);
    }

    @Override
    public Employee updateRecentUse(String employee_id, String app_id, Integer tenant_id) {
        String current = employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id).getLast_use_app_id();
        if(current == null || current.equals("")) {
            employeeDao.updateLastUseApp(employee_id, app_id + " & " + System.currentTimeMillis(), tenant_id);
        } else {
            List<String> apps = Arrays.asList(current.split(","));
            boolean ifHas = false;
            String newString = "";
            for (String app : apps) {
                String currentApp = app.split(" & ")[0];
                if(app_id.equals(currentApp)) {
                    ifHas = true;
                    newString = currentApp + " & " + System.currentTimeMillis();
                }
            }
            if(ifHas) {
                for (String app : apps) {
                    if(!app.split(" & ")[0].equals(app_id)) {
                        newString += "," + app;
                    }
                }
                employeeDao.updateLastUseApp(employee_id, newString, tenant_id);
            } else {
                if(apps.size() < 5) {
                    employeeDao.updateLastUseApp(employee_id, current + "," + app_id + " & " + System.currentTimeMillis(), tenant_id);
                } else {
                    List<String> newApps = new ArrayList<>();
                    newApps.add(app_id + " & " + System.currentTimeMillis());
                    newApps.addAll(apps);
                    List<String> newApp = new ArrayList<>();
                    for (int i = 0; i < newApps.size(); i++) {
                        if(i != newApps.size() - 1) {
                            newApp.add(newApps.get(i));
                        }
                    }
                    employeeDao.updateLastUseApp(employee_id, String.join(",", newApp), tenant_id);
                }
            }
        }
        Employee res = employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id);
        res = organizationalStructureService.getAllKindsDept(res);
        res = loginService.getLastSelectedApp(res);
        return res;
    }

    @Override
    public Employee updateSelected(String app_id, Integer mode, String employee_id, Integer tenant_id) {
        Employee employee = employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id);
        String selectedApp = employee.getSelected_app_id();
        String addString = "";
        if(mode == 1) {
            if(selectedApp == null || selectedApp.equals("")) {
                addString = app_id;
            } else {
                List<String> apps = Arrays.asList(selectedApp.split(","));
                if(apps.size() > 4) {
                    return null;
                } else {
                    if(!apps.contains(app_id)) {
                        addString = selectedApp + "," + app_id;
                    }
                }
            }
        } else if(mode == 0) {
            String[] apps = selectedApp.split(",");
            for (String app : apps) {
                if(!app.equals(app_id)) {
                    addString = addString.equals("") ? app : addString + "," +app;
                }
            }
        }
        employeeDao.updateSelectedApp(addString, employee_id, tenant_id);
        Employee res = employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id);
        res = organizationalStructureService.getAllKindsDept(res);
        return(loginService.getLastSelectedApp(res));
    }
}
